package com.huawei.appmarket.service.externalapi.secureactivity;

import android.app.Activity;
import android.app.Application.ActivityLifecycleCallbacks;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.foundation.ui.framework.activity.BaseActivity;
import com.huawei.secure.android.common.intent.SafeBundle;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import o.cjm;
import o.djk;
import o.Logger;
import o.dpi;
import o.dyv;
import o.dyz;

public class SecureProxyActivity extends BaseActivity implements ActivityLifecycleCallbacks {
    private int ʻॱ;
    private boolean ˊॱ = false;
    private boolean ˋॱ = false;
    private boolean ˏॱ = false;
    private boolean ͺ = false;
    private boolean ॱˊ = false;
    private int ॱˋ;
    private dyv ॱˎ;
    private final Map<Integer, b> ᐝॱ = new HashMap();

    static final class b {
        private WeakReference<Activity> ˊ;
        private boolean ˋ;
        private boolean ˎ;

        private b() {
        }
    }

    static {
        dpi.ˎ(SecureProxyActivity.class);
    }

    public void onCreate(@Nullable Bundle bundle) {
        super.onCreate(bundle);
        djk.ˋ().ˎ(SecureProxyActivity.class);
        this.ʻॱ = getTaskId();
        SafeIntent safeIntent = new SafeIntent(getIntent());
        this.ˋॱ = dyz.ॱ(this, safeIntent.getStringExtra("verify_legality_of_caller"), getCallingPackage());
        Logger.ˎ("SecureProxyActivity", "start SecureProxyActivity result:" + this.ˋॱ);
        if (this.ˋॱ) {
            if (safeIntent.getIntExtra("agd.extra.autofinish", 0) == 1) {
                this.ˊॱ = true;
            }
            if (this.ˊॱ) {
                SafeBundle safeBundle = new SafeBundle(safeIntent.getBundleExtra("agd.extra.bundle"));
                this.ॱˋ = safeBundle.getInt("agd.extra.bundle.requestcode");
                this.ॱˎ = o.dyv.b.ˋ(safeBundle.getBinder("agd.extra.bundle.binder"));
                getApplication().registerActivityLifecycleCallbacks(this);
                return;
            }
            finish();
            return;
        }
        setResult(0);
        finish();
    }

    public void onResume() {
        super.onResume();
        if (this.ˊॱ) {
            if (this.ॱˊ && this.ˋॱ) {
                Logger.ˎ("SecureProxyActivity", "auto finish activity when onResume");
                this.ˏॱ = true;
                finish();
            }
            this.ॱˊ = true;
        }
    }

    private void ᐝ() {
        if (this.ॱˎ != null) {
            Logger.ˎ("SecureProxyActivity", "onActivityCancel:" + this.ॱˋ);
            try {
                this.ॱˎ.ॱ(this.ॱˋ);
            } catch (RemoteException e) {
                Logger.e("SecureProxyActivity", "can not onActivityCancel:RemoteException");
            } catch (Exception e2) {
                Logger.e("SecureProxyActivity", "can not onActivityCancel" + e2.getMessage());
            }
        }
    }

    public void onStart() {
        super.onStart();
        this.ͺ = false;
    }

    public void onStop() {
        super.onStop();
        this.ͺ = true;
        boolean ͺ = ͺ();
        if (this.ˊॱ && ͺ && !isFinishing()) {
            Logger.ˎ("SecureProxyActivity", "auto finish activity when onStop");
            if (!ॱˊ()) {
                ᐝ();
                this.ˏॱ = true;
            }
            finish();
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.ˊॱ) {
            getApplication().unregisterActivityLifecycleCallbacks(this);
        }
        if (this.ˏॱ) {
            for (b bVar : this.ᐝॱ.values()) {
                if (!(bVar == null || bVar.ˎ)) {
                    Activity activity = (Activity) bVar.ˊ.get();
                    if (!(activity == null || activity.isFinishing())) {
                        activity.finish();
                    }
                }
            }
        }
        this.ᐝॱ.clear();
    }

    public void onActivityCreated(Activity activity, Bundle bundle) {
        if (activity.getTaskId() == this.ʻॱ) {
            b bVar = new b();
            bVar.ˊ = new WeakReference(activity);
            this.ᐝॱ.put(Integer.valueOf(activity.hashCode()), bVar);
        }
    }

    public void onActivityStarted(Activity activity) {
        if (activity.getTaskId() == this.ʻॱ) {
            b bVar = (b) this.ᐝॱ.get(Integer.valueOf(activity.hashCode()));
            if (bVar != null) {
                bVar.ˋ = true;
            }
        }
    }

    public void onActivityResumed(Activity activity) {
        if (activity.getTaskId() == this.ʻॱ) {
            b bVar = (b) this.ᐝॱ.get(Integer.valueOf(activity.hashCode()));
            if (bVar != null) {
                bVar.ˎ = true;
            }
        }
    }

    public void onActivityPaused(Activity activity) {
        if (activity.getTaskId() == this.ʻॱ) {
            b bVar = (b) this.ᐝॱ.get(Integer.valueOf(activity.hashCode()));
            if (bVar != null) {
                bVar.ˎ = false;
            }
        }
    }

    public void onActivityStopped(Activity activity) {
        if (activity.getTaskId() == this.ʻॱ) {
            b bVar = (b) this.ᐝॱ.get(Integer.valueOf(activity.hashCode()));
            if (bVar != null) {
                bVar.ˋ = false;
            }
        }
        if (this.ͺ && !isFinishing() && ͺ() && ॱˊ()) {
            Logger.ˎ("SecureProxyActivity", "auto finish activity when upper activity onStop");
            ᐝ();
            this.ˏॱ = true;
            finish();
        }
    }

    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    public void onActivityDestroyed(Activity activity) {
        if (activity.getTaskId() == this.ʻॱ) {
            this.ᐝॱ.remove(Integer.valueOf(activity.hashCode()));
        }
    }

    private boolean ॱˊ() {
        for (b ˏ : this.ᐝॱ.values()) {
            if (ˏ.ˋ) {
                return false;
            }
        }
        return true;
    }

    private boolean ͺ() {
        for (b ˋ : this.ᐝॱ.values()) {
            if (ˋ.ˎ) {
                return false;
            }
        }
        return true;
    }

    public static PendingIntent ˎ(Context context, @NonNull String str, Intent intent) {
        return ˎ(context, dyz.ˋ(str, intent, null, null));
    }

    public static PendingIntent ˎ(Context context, @NonNull String str, cjm o_cjm) {
        return ˎ(context, dyz.ˋ(str, null, null, o_cjm));
    }

    private static PendingIntent ˎ(Context context, String str) {
        Intent intent = new Intent(context.getPackageName() + ".service.intent.SECURE_PROXY_ACTIVITY");
        intent.setPackage(context.getPackageName());
        intent.putExtra("verify_legality_of_caller", str);
        return PendingIntent.getActivity(context, str.hashCode(), intent, 1073741824);
    }

    public void ᐝᐝ() {
    }
}
